package SiTuQingYou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import SiTuQingYou.util.DBHelper;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.Comment;
import SiTuQingYou.vo.Strategy;
import SiTuQingYou.vo.User;

public class CommentDao 
{
	private  CommentDao() {}
	private static  CommentDao cd= new  CommentDao();
	public static  CommentDao getcd()
	{
		return cd;
	}
	//添加一条新的评论
	public void addComment(int userId,int strategyId,String text)
	{
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn=DBHelper.getDB().getConn();
			String sql="insert into comment(userid,strategyid,comment)value(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, strategyId);
			ps.setString(3, text);
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			DBHelper.getDB().close(rs, ps, conn);
		}
	}
	//删除评论
	public void deletecom(int id) {
			Connection conn=null;
			PreparedStatement ps=null;
			ResultSet rs=null;
			
			try {
				conn = DBHelper.getDB().getConn();
				String sql = "update comment set fakedelete=1 where id=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, id);
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDB().close(rs, ps, conn);
			}
	}
	//该攻略下全部评论
	 public ArrayList<Comment> getallcomment (int stratrgyid){
		 	Connection conn=null;
			PreparedStatement ps=null;
			ResultSet rs=null;
			ArrayList<Comment> allcomment = new ArrayList<Comment>();
			try {
				conn = DBHelper.getDB().getConn();
				String sql = "select *  from comment,user,strategy where  user.id=comment.userid and strategyid=strategy.id and strategyid=? and comment.fakedelete=0 order by commenttime desc";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, stratrgyid);
				rs = ps.executeQuery();
				while(rs.next()) {
					Comment com = new Comment();
					User use = new User();
					use.setUsernickname(rs.getString("usernickname"));
					use.setUseranswer(rs.getString("useranswer"));
					com.setId(rs.getInt("id"));
					com.setUser(use);
					com.setCommenttime(rs.getTimestamp("commenttime"));
					com.setComment(rs.getString("comment"));
					allcomment.add(com);
				}
				return allcomment;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDB().close(rs, ps, conn);
			}
			return null;
	 }
	
	//通过用户id，查询出他的所有评论
	public ArrayList<Comment> lookForAllMyComment(int userid,Pager pager)
	{
		pager.setDataCount(this.getCommentNumber(userid));
		
		ArrayList<Comment> allComment = new ArrayList<Comment>();
		
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		
		
		try 
		{
			conn = DBHelper.getDB().getConn();
			String sql = "select * from comment,strategy WHERE comment.userid=? and strategyid=strategy.id "
					+ "and comment.fakedelete=0 ORDER BY commenttime desc limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1,userid);
			ps.setInt(2,pager.getFirstRow());//获得limit后面第一个数是几
			ps.setInt(3,pager.getPageSize());//获得每页几条记录
			rs = ps.executeQuery();
			while(rs.next()) {
				Comment ct = new Comment();
				ct.setComment(rs.getString("comment"));//评论内容
				ct.setCommenttime(rs.getTimestamp("commenttime"));//评论时间
				ct.setId(rs.getInt("comment.id"));
				Strategy str = new Strategy();
				str.setId(rs.getInt("strategy.id"));
				str.setPhoto(rs.getString("photo"));//评论对应的攻略图片
				str.setTitle(rs.getString("title"));//评论对应的攻略题目
				ct.setStrategy(str);
				
				allComment.add(ct);
			}
			return allComment;
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			DBHelper.getDB().close(rs, ps, conn);
		}
		return null;
		
	}
	
	
	
	//查询出此用户总共有多少条评论
	public int getCommentNumber(int userid)
	{
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int count = 0;
		
		
		try 
		{
			conn = DBHelper.getDB().getConn();
			String sql = "select count(id) as commentNum from comment where userid=? and fakedelete=0";
			ps = conn.prepareStatement(sql);
			ps.setInt(1,userid);
			rs = ps.executeQuery();
			if(rs.next()) {
				count = rs.getInt("commentNum");
			}
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			DBHelper.getDB().close(rs, ps, conn);
		}
		return count;
		
	}
	
	//删除(假删除)选中的评论
	public void deleteAllCheckedComment(String cid[])
	{
		String s = "";
		for(String id:cid)
		{
			s = s+","+id;
		}
		s = s.substring(1);
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try 
		{
			conn = DBHelper.getDB().getConn();
			
			String sql = "update comment set fakedelete=1 where id in("+s+")";
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			DBHelper.getDB().close(rs, ps, conn);
		}
	}
	
}
